﻿#ifndef QCPUMONITOR_H
#define QCPUMONITOR_H

#include <QObject>
#include <QtGlobal>

#ifdef Q_OS_WIN
#include <windows.h>
#else
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <unistd.h>
#endif

class QCpuMonitor {
public:
    QCpuMonitor();
    ~QCpuMonitor();

    // 获取系统级CPU使用率（0-100%）
    double systemUsage();

    // 获取进程级CPU使用率（pid=0表示当前进程）
    double processUsage(qint64 pid = 0);

private:
#ifdef Q_OS_WIN
    struct WinData {
        ULARGE_INTEGER lastIdleTime;
        ULARGE_INTEGER lastKernelTime;
        ULARGE_INTEGER lastUserTime;
        bool initialized = false;
    };
    WinData m_winData;
#else
    struct LinuxData {
        quint64 lastTotal;
        quint64 lastIdle;
        bool initialized = false;
    };
    LinuxData m_linuxData;
#endif
    qint64 m_processorCount;
};
#endif // QCPUMONITOR_H
